﻿using System.Collections.Generic;
using Inovout.Business;
using Inovout.Descriptors;

namespace Inovout.Gui.Wpf.Controls
{
    public static class MenuDescriptor
    {

        public static List<MenuItemViewModel> Build()
        {
            List<MenuItemViewModel> menuItems = BuildFromXml();
            if (menuItems == null)
            {
                menuItems = BuildFromFunction();
            }

            return menuItems;
        }

        private static List<MenuItemViewModel> BuildFromFunction()
        {
            List<MenuItemViewModel> menuItems = new List<MenuItemViewModel>();
            foreach (FunctionGroup functionGroup in FunctionGroup.Application.Functions)
            {
                MenuItemViewModel menuItem = CreateMenuItem(functionGroup);
                menuItem.Items = CreateMenuItems(functionGroup);
                menuItems.Add(menuItem);
            }
            return menuItems;
        }
        private static MenuItemViewModel CreateMenuItem(FunctionGroup functionGroup)
        {
            //ClassDescriptor classDescriptor = ClassDescriptor.Create(functionGroup.Owner.GetType());
            //FieldDescriptor fieldDescriptor = classDescriptor.GetField(functionGroup.Name);
            return new MenuItemViewModel(functionGroup);

        }

        private static List<MenuItemViewModel> CreateMenuItems(FunctionGroup functionGroup)
        {
            List<MenuItemViewModel> menuItems = new List<MenuItemViewModel>();
            foreach (IFunction function in functionGroup.Functions)
            {
                MenuItemViewModel menuItem = null;
                if (function is FunctionGroup)
                {
                    menuItem = CreateMenuItem(function as FunctionGroup);
                    menuItem.Items = CreateMenuItems(function as FunctionGroup);
                }
                else
                {
                    menuItem = new MenuItemViewModel(function);
                    //{
                    //    Name = GetFunctionAlais(function as Function),
                    //    HotKey=function.HotKey,
                    //    Command = new WorkItemCommand(function as Function)
                    //};

                }
                menuItems.Add(menuItem);
            }
            return menuItems;
        }

        private static string GetFunctionAlais(Function function)
        {
            FunctionGroup[] functionGroups = ComponentRegistry.ResolveAll<FunctionGroup>();
            ClassDescriptor functionClassDescriptor = ClassDescriptor.Create(typeof(Function));
            foreach (FunctionGroup functionGroup in functionGroups)
            {
                ClassDescriptor functionGroupClassDescriptor = ClassDescriptor.Create(functionGroup.GetType());
                FieldDescriptor functionFieldDescriptor = functionGroupClassDescriptor.GetField(function.Name);
                if (functionFieldDescriptor != null)
                {
                    if (functionClassDescriptor.GetProperty("WorkItemType").GetValue(functionFieldDescriptor.GetValue()).Equals(function.WorkItemType))
                    {
                        return functionFieldDescriptor.Alias;
                    }
                }
            }
            return function.Name;
        }

        private static List<MenuItemViewModel> BuildFromXml()
        {
            return null;
        }
    }
}
